<?php

namespace app\api\controller;

use think\facade\Db;

class System extends ApiBase
{
    // 不需要验证token的方法
    protected $middleware = [
        'app\api\middleware\Auth' => ['except' => ['cloud','miniConfig']],
    ];

    public function baseSet()
    {
        $appid = input("appid");
        $secret = input("secret");
        $intro = input("intro");
        $data["value"] = $appid;
        $data["extend"] = $secret;
        $data["intro"] = $intro;
        $res = Db::name("system")->where("config", "miniapp")->update($data);
        if ($res) {
            return notice("修改成功");
        }
        return error("修改失败");
    }

    //微信小程序配置
    public function miniConfig()
    {
        $miniApp = Db::name("system")->where("config", "miniapp")->find();
        return success($miniApp);
    }

    //上传配置
    public function uploadConfig()
    {
        $uploadType = Db::name("system")->where("config", "uploadType")->find();
        return success($uploadType);
    }
    public function uploadType()
    {
        $data = input("post.");
        if ($data["uploadType"] == 1) {
            $insertData = ["value" => $data["uploadType"]];
        } elseif ($data["uploadType"] == 2) {
            $insertData = ["value" => $data["uploadType"], "extend" => json_encode(["endpoint" => $data["OssAddress"], "OssName" => $data["OssName"], "accessKeyId" => $data["keyId"], "accessKeySecret" => $data["keySecret"]])];
        }
        $update = Db::name("system")->where("config", "uploadType")->update($insertData);
        if ($update) {
            return notice("保存成功");
        }
        return error("保存失败");
    }

    public function Statistics()
    {
        $data['userCount'] = Db::name("user")->count();
        $data['postCount'] = Db::name("post")->count();
        $data['disCount'] = Db::name("discuss")->count();
        $data['commentCount'] = Db::name("comment")->count();

        $data["last_month"] = Db::name('user')->whereMonth('create_time', 'last month')->count();
        $data["month"] = Db::name('user')->whereMonth('create_time')->count();
        $data["week"] = Db::name('user')->whereWeek('create_time')->count();
        $data["day"] = Db::name('user')->whereDay('create_time')->count();

        //近30天日期列表
        $monthDateList = getDateInterval(30, '-');
        $monthValue = [];

        foreach ($monthDateList as $item) {
            $date = Db::name('user')->whereDay('create_time', $item)->count();
            array_push($monthValue, $date);
        }

        $month_data = [
            "lable" => $monthDateList,
            "value" => $monthValue
        ];

        //用户性别统计
        $unknown = Db::name('user')->where("gender", 0)->count();
        $man = Db::name('user')->where("gender", 1)->count();
        $female = Db::name('user')->where("gender", 2)->count();

        $data["month_data"] = $month_data;
        $data["user_gender"] = [
            [
                "name" => "男性",
                "value" => $man,
            ],
            [
                "name" => "女性",
                "value" => $unknown,
            ],
            [
                "name" => "未知",
                "value" => $female,
            ]
        ];

        //用户城市分布
        $cityList = [];
        $userList = Db::name('user')->cache(true, 3600)->column("province");

        foreach ($userList as $item) {
            if(!empty($item)){
                if(!in_array("$item",$cityList)){
                    array_push($cityList, $item);
                }
            }
        }

        $cityList = array_unique($cityList);

        $userCity = [];
        foreach ($cityList as $item) {
            $count = Db::name('user')->where('province', $item)->cache(true, 3600)->count();
            $cityArr = [
                "value" => $count
            ];
            array_push($userCity, $cityArr);
        }
        $data["user_city"] = [
            "lable" => $cityList,
            "value" => $userCity
        ];
        return success($data);
    }

    //系统版本信息
    public function sysInfo(){
        $info = [
            "sys_version" => SYSTEM_VERSION,
            "php_version"  => PHP_VERSION,
            "PHP_OS"  => PHP_OS,
            "SERVER_NAME" => $_SERVER['SERVER_NAME']
        ];

        return success($info);
    }

    //获取云服务信息
    public function cloud(){

        $url = "https://cloud.ymeoo.cn/api/system/sysInfo";

        $data["domain"] = request()->host();
        $data["version"] = SYSTEM_VERSION;
        $request = httpRequest($url,$data,"POST");
        return success($request);
    }
}